import axios from 'axios'
import nProgress from 'nprogress'
import store from '@/store'
import 'nprogress/nprogress.css'
const request = axios.create({
    baseURL:process.env.VUE_APP_API,
    timeout:10000
})

request.interceptors.request.use((config) => {
        nProgress.start()
        //每次发送请求的时候,在header中携带用户的临时标识
        const {userTempId} = store.state.user
        config.headers.userTempId = userTempId
         //每次发送请求的时候,在header中携带token
        const {token} = store.state.user
        config.headers.token = token;
        return config
},(error) => {
    nProgress.done()
    return Promise.reject(error)
})

request.interceptors.response.use((response) => {
    nProgress.done()
    if(response.data.code === 200){
        return response.data.data
    }else{
        return Promise.reject({message:response.data.message})
    }
},(error) => {
    nProgress.done()
    return  Promise.reject(error)
})
export default request